package ee.ut.cs.mc.madp.p2pdivider.adapter;

import java.util.List;

import android.content.Context;
import android.net.wifi.p2p.WifiP2pDevice;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.TextView;
import ee.ut.cs.mc.madp.p2pdivider.R;
import ee.ut.cs.mc.madp.p2pdivider.fragment.DevicesFragment;

/**
 * Adapter for displaying WifiP2pDevices in ListView
 * 
 * @author Kaarel Jõgeva
 */
public class DeviceListAdapter extends ArrayAdapter<WifiP2pDevice> {

    final private static String TAG = DeviceListAdapter.class.getSimpleName();

    private final List<WifiP2pDevice> devices;
    private final DevicesFragment fragment;

    public DeviceListAdapter(Context context, int textViewResourceId, DevicesFragment fragment, List<WifiP2pDevice> objects) {
        super(context, textViewResourceId, objects);
        devices = objects;
        this.fragment = fragment;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View v = convertView;
        if (v == null) {
            LayoutInflater vi = (LayoutInflater) fragment.getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = vi.inflate(R.layout.view_device_list_row, null);
        }
        final WifiP2pDevice device = devices.get(position);
        if (device != null) {
            CheckBox checkBox = (CheckBox) v.findViewById(R.id.deviceListCheckbox);
            TextView info = (TextView) v.findViewById(R.id.deviceListInfo);
            if (checkBox != null) {
                checkBox.setText(device.deviceName);
                checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {

                    @Override
                    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                        if (isChecked) {
                            Log.d(TAG, "Adding device with name " + device);
                            fragment.connectTo(device);
                        } else {
                            fragment.disconnectFrom(device);
                        }
                    }
                });
            }
            if (info != null) {
                info.setText(getDeviceStatus(device.status));
            }
        }

        return v;
    }

    private static String getDeviceStatus(int deviceStatus) {
        Log.d(TAG, "Peer status :" + deviceStatus);
        switch (deviceStatus) {
        case WifiP2pDevice.AVAILABLE:
            return "Available";
        case WifiP2pDevice.INVITED:
            return "Invited";
        case WifiP2pDevice.CONNECTED:
            return "Connected";
        case WifiP2pDevice.FAILED:
            return "Failed";
        case WifiP2pDevice.UNAVAILABLE:
            return "Unavailable";
        default:
            return "Unknown";

        }
    }

}